package com.yunc.common.thread.factory;

import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yunc.common.exception.UCException;
import com.yunc.common.utils.BaseConstant;
import com.yunc.common.utils.SpringContextUtils;
import com.yunc.modules.chat.entity.ChatMessages;
import com.yunc.modules.chat.service.IChatMessagesService;
import com.yunc.modules.shop.entity.Address;
import com.yunc.modules.shop.entity.Item;
import com.yunc.modules.shop.entity.Order;
import com.yunc.modules.shop.entity.OrderItem;
import com.yunc.modules.shop.entity.vo.OrderItemVO;
import com.yunc.modules.shop.entity.vo.OrderVO;
import com.yunc.modules.shop.service.IAddressService;
import com.yunc.modules.shop.service.IItemService;
import com.yunc.modules.shop.service.IOrderItemService;
import com.yunc.modules.shop.service.IOrderService;
import com.yunc.modules.sys.entity.SysLog;
import com.yunc.modules.sys.service.ISysLogService;

import java.util.Date;
import java.util.List;
import java.util.TimerTask;

/**
 * 异步工厂（产生任务用）
 * 
 * @author liuhulu
 *
 */
public class AsyncFactory {

    /**
     * 操作日志记录
     */
    public static TimerTask recordSysLog(final SysLog sysLog) {
        return new TimerTask() {
            @Override
            public void run() {
                ISysLogService sysLogService = (ISysLogService)SpringContextUtils.getBean("sysLogService");
                sysLogService.save(sysLog);
            }
        };
    }

    public static TimerTask insertOrder(OrderVO orderInfo) {
        return new TimerTask() {
            @Override
            public void run() {
                IAddressService addressService = (IAddressService)SpringContextUtils.getBean("addressService");
                IOrderService orderService = (IOrderService)SpringContextUtils.getBean("orderService");
                IItemService itemService = (IItemService)SpringContextUtils.getBean("itemService");
                IOrderItemService orderItemService = (IOrderItemService)SpringContextUtils.getBean("orderItemService");
                // 生成订单
                Order order = new Order();
                order.setMemberId(orderInfo.getMemberId());
                order.setOrderNumber(orderInfo.getOrderNumber());
                order.setStatus(BaseConstant.OrderStatus.PAY.getValue());
                order.setPaymentTime(new Date());
                order.setPayment(orderInfo.getPayment());
                order.setAddressId(orderInfo.getAddressId());
                // 地址
                Address address = addressService.getById(orderInfo.getAddressId());
                if (address != null) {
                    order.setAddressName(address.getName());
                    order.setAddressPhone(address.getPhone());
                    order.setAddressDescripts(address.getDescripts());
                }
                order.setRemoveTag(BaseConstant.REMOVE_Y);
                order.setCreateTime(new Date());
                boolean flag = orderService.save(order);
                // order表的主键
                if (flag) {
                    List<OrderItemVO> orderItemList = JSON.parseArray(orderInfo.getOrderItemJson(), OrderItemVO.class);
                    if (orderItemList != null && orderItemList.size() > 0) {
                        for (OrderItemVO orderItemD : orderItemList) {
                            Item item = itemService.getById(orderItemD.getItemId());
                            if (item == null) {
                                throw new UCException("未查询到购买商品");
                            }
                            OrderItem orderItem = new OrderItem();
                            orderItem.setOrderId(order.getId());
                            orderItem.setItemId(orderItemD.getItemId());
                            orderItem.setSpecsId(orderItemD.getSpecsSeq());
                            orderItem.setNum(orderItemD.getNum());
                            orderItem.setParamData(orderItemD.getParamData());
                            orderItem.setTitle(item.getTitle());
                            orderItem.setPrePrice(item.getPrePrice());
                            orderItem.setOrigPrice(item.getOrigPrice());
                            orderItem.setImage(item.getImage());
                            orderItem.setTotalPrice(item.getPrePrice() * orderItemD.getNum());
                            orderItem.setCreateTime(new Date());
                            orderItemService.save(orderItem);
                        }
                    }
                }
            }
        };
    }

    public static TimerTask insertFriendMessages(Long fromId, Long toId, String postMessage) {
        return new TimerTask() {
            @Override
            public void run() {
                IChatMessagesService chatMessagesService = (IChatMessagesService)SpringContextUtils.getBean("chatMessagesService");
                ChatMessages record = chatMessagesService.getOne(new QueryWrapper<ChatMessages>().eq("from_user_id", fromId)
                        .eq("to_user_id", toId));
                if (record != null) {
                    return;
                }
                ChatMessages chatMessages = new ChatMessages();
                chatMessages.setFromUserId(fromId);
                chatMessages.setToUserId(toId);
                chatMessages.setType("2");
                chatMessages.setStatus("1");
                chatMessages.setSendTime(DateUtil.date());
                chatMessages.setPostMessage(postMessage);
                chatMessagesService.save(chatMessages);
            }
        };
    }
}
